'''
    @project: Masdevallia
    @author: Austin Baker
    @created: 2/22/2014
    @license: http://opensource.org/licenses/mit-license.php

    The MIT License (MIT)

    Copyright (c) 2014 Austin Baker
    
    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:
    
    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.
    
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
    
    ------
    
    About: Edit modules widget.
'''

'''
============================================================
--->>   IMPORT MODULES
============================================================
'''
import importlib

import maya.OpenMaya as OpenMaya

import masdevallia.settings.PathConstants as PathConstants
import masdevallia.utility.WindowUtility as WinUtility
import masdevallia.utility.Dag as Dag
import masdevallia.utility.General as General
import masdevallia.nodes.Node as Node

from PySide import QtCore
from PySide import QtGui

'''
============================================================
--->>   GLOBAL VARIABLES
============================================================
'''
form_class, base_class = WinUtility.loadUiType( PathConstants.WIDGET_EDIT_MODULES_GUI )

'''
============================================================
--->>   EDIT MODULES CLASS
============================================================
'''
class EditModules( form_class, base_class ):
    def __init__( self, parent=None ):
        super( EditModules, self).__init__( parent )
        self.setupUi( self )
        self.setObjectName( 'Edit_Modules' )
        
        # Stores the currently selected module as a Node.
        self.current_module = None
        self.current_meta_node = None
        
        self.initConnections()
        
    def initConnections( self ):
        '''
        Initial GUI connections.
        '''
        self.module_refresh.clicked.connect( self.refreshModule )
                
    def refreshModule( self ):
        '''
        Refreshes the selected module.
        '''
        sel_list = OpenMaya.MSelectionList()
        OpenMaya.MGlobal.getActiveSelectionList( sel_list )
        
        if sel_list.length() == 1:
            obj = OpenMaya.MObject()
            sel_list.getDependNode( 0, obj )
            meta_plug = Dag.getPlugByName( obj, 'metaNode' )
            
            if meta_plug is not None:
                meta_connections = Dag.getPlugConnections( meta_plug, False, True )
                
                if meta_connections is not None:
                    # Get an instance of the module MetaNode.
                    meta_node_name = meta_connections[0].name().split( '.' )[0]
                    meta_module = importlib.import_module( 'masdevallia.meta.{0}'.format( meta_node_name ) )
                    meta_class = getattr( meta_module, meta_node_name )
                    self.current_meta_node = meta_class( node_name=meta_node_name )    
                    print self.current_meta_node
                
                    # Get the RigNodule and update the label.                    
                    self.current_module = self.current_meta_node.getModule()                    
                    print self.current_module
                    
                    self.module_label.setText( self.current_module.name )

                    self.updatePartsWidgets()
     
    def updatePartsWidgets( self ):
        '''
        Refreshes the modules parts (markers, etc.) editor guis.
        '''
        # Clear the layout.
        
        # Add the new widgets.
        self.current_module.getEditorUi( self.tile_layout )
        
        for widget_idx in xrange( self.tile_layout.count() ):
            print widget_idx
            widget_item = self.tile_layout.itemAt( widget_idx )
            print widget_item
            print widget_item.widget()
